<template>
  <div class="list-container">
    <!-- <el-divider>展示所有用户信息，可根据手机号操作查询</el-divider> -->
    <vab-query-form>
      <el-form
        ref="form"
        :model="queryForm.queryBean"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.phone"
            placeholder="手机号码"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.userName"
            placeholder="用户名"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="queryForm.queryBean.roleId"
            placeholder="用户类型"
          >
            <el-option label="游客" value="1"></el-option>
            <el-option label="业主" value="2"></el-option>
            <el-option label="驻场人员" value="4"></el-option>
            <el-option label="开发商" value="3"></el-option>
            <el-option label="管理员" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <el-table-column
        show-overflow-tooltip
        prop="userName"
        label="用户名称"
        width="100"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="手机号码"
        prop="phone"
        width="120"
      ></el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        label="用户ID"
        prop="userId"
      ></el-table-column> -->
      <el-table-column
        show-overflow-tooltip
        label="用户类型"
        prop="roleId"
        :formatter="formatterType"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="300">
        <template #default="{ row }">
          <!-- <el-button
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(row)"
          ></el-button> -->
          <el-button
            v-if="row.roleId != 5"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
          <!-- <el-button type="text" >删除</el-button> -->
          <el-button
            v-if="row.roleId == 3"
            type="primary"
            @click="handleSet(row)"
          >
            地产信息
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!-- <table-edit ref="edit">修改表单 </table-edit> -->
    <set-company ref="set" @fetch-data="fetchData"></set-company>
  </div>
</template>
<script>
  import { getUserList, deleteUser, deleteDeveloperInfo } from '@/api/user'
  import store from '@/store'
  import SetCompany from './components/SetCompany.vue'

  export default {
    name: 'List',
    components: { SetCompany },
    data() {
      return {
        imgShow: true,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {},
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    mounted() {},
    methods: {
      handleSet(row) {
        if (row.userId) {
          this.$refs['set'].showEdit(row)
        }
      },
      formatterType(row) {
        let type = row.roleId
        switch (type) {
          case '1':
            return '游客'
          case '2':
            return '业主'
          case '3':
            return '开发商'
          case '4':
            return '驻场人员'
          case '5':
            return '管理员'
        }
      },
      // handleAdd() {
      //   this.$refs['edit'].showEdit()
      // },
      // handleEdit(row) {
      //   this.$refs['edit'].showEdit(row)
      // },
      async deleteDevelopRecord(_phone) {
        const { data: res } = await deleteDeveloperInfo({
          phone: _phone,
        })
        if (res.status == 0) {
          this.$baseMessage('删除开发商成功！', 'success')
        } else {
          this.$baseMessage(res.message, 'error')
        }
      },
      handleDelete(row) {
        if (row.userId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { data } = await deleteUser({ userId: row.userId })
            if (data.status == 0) {
              if (row.roleId == 3) {
                this.deleteDevelopRecord(row.phone)
                //开发商删除对应审核记录
              } else {
                this.$baseMessage('删除成功！', 'success')
              }
            } else {
              this.$baseMessage(data.message, 'error')
            }
            this.fetchData()
          })
        } else {
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getUserList(this.queryForm)
        const { body } = data
        this.list = body.data
        this.total = body.totalSize
        this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
